package com.teacher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.auto.JSONAuthentication;
import com.teacher.entity.TClass;
import com.teacher.entity.TXj;
import com.teacher.entity.TXk;
import com.teacher.mapper.TXjMapper;
import com.teacher.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author teacher
 * @since 2022-04-17
 */
@RestController
@RequestMapping("/t-xj")
public class TXjController extends JSONAuthentication {
    @Autowired
    TXjMapper tXjMapper;

    @ApiOperation(value = "添加学生学籍信息")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody TXj tXj) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("sfz",tXj.getSfz());
        if(tXjMapper.selectOne(wrapper)!=null){
            Result error = Result.error().message("该身份证已存在");
            this.WriteJSON(request, response, error);
        }else{
            if (tXjMapper.insert(tXj)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error().message("添加失败");
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除学籍")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = tXjMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找所有学籍")
    @PostMapping("/findAllXk")
    public void findAllXk(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<TXj> shUserPage = new Page<>(current, size);
        Page<TXj> shUserPages = tXjMapper.selectPage(shUserPage, null);
        List<TXj> records = shUserPages.getRecords();
        int total = tXjMapper.selectCount(null);
        if (records.size() > 0) {
            Result ok = Result.ok().data("Xj", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

